package com.edfu.core.commons.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.edfu.core.commons.beans.Category;
import com.edfu.core.commons.persistence.entity.CategoryEntity;

public class CategoryMgtBeanTransformer {
	
	public static Category transformCategory(CategoryEntity categoryEntity) {
		Category tc = convertCategory(categoryEntity);
		
		if (categoryEntity.getParentCategory() != null) {
			tc.setParentCategory(convertCategory(categoryEntity.getParentCategory()));
		}
		
		return tc;
	}

	private static Category convertCategory(CategoryEntity categoryEntity) {
		Category tc = new Category();
//		tc.setChildCategories(childCategories);
		tc.setDescription(categoryEntity.getDescription());
		tc.setId(categoryEntity.getId());
		tc.setName(categoryEntity.getName());
		
		return tc;
	}

	public static CategoryEntity transformCategory(Category category) {
		CategoryEntity tce = new CategoryEntity();
		tce.setDescription(category.getDescription());
		tce.setName(category.getName());
//		tce.setParentCategory(parentCategory);
		
		return tce;
	}

	public static CategoryEntity transformCategory(CategoryEntity categoryEntity, Category category) {
		categoryEntity.setDescription(category.getDescription());
		categoryEntity.setName(category.getName());
		
		return categoryEntity;
	}

	public static List<Category> transformCategory(List<CategoryEntity> ttcList) {
		List<Category> tcList = new ArrayList<Category>();
		for (CategoryEntity tce : ttcList) {
			tcList.add(transformCategory(tce));			
		}
		
		return tcList;
	}

	public static List<CategoryEntity> transformCategoryBeans(List<Category> categories) {
		List<CategoryEntity> tceList = new ArrayList<CategoryEntity>();
		
		for (Category tc : categories) {
			tceList.add(transformCategory(tc));
		}
		return tceList;
	}

	public static List<Category> getHierarchicalCategories(List<Category> flatList) {
		List<Category> result = new ArrayList<Category>();
		Map<String, Category> map = new HashMap<String, Category>();
		for(Category tc : flatList)
			map.put(tc.getName(), tc);
				
		for(Category category : flatList){
			if(category.getName().equalsIgnoreCase(category.getParentCategory().getName()))
				result.add(category);
			else {
				List<Category> childCategories = map.get(category.getParentCategory().getName()).getChildCategories();
				if(childCategories == null || childCategories.size() == 0) {
					childCategories = new ArrayList<Category>();
					childCategories.add(category);
				}
				else 
					childCategories.add(category);
				
				map.get(category.getParentCategory().getName()).setChildCategories(childCategories);
			}
		}
		return result;
	}

	public static Set<Category> transformCategory(Set<CategoryEntity> categoryEntities) {
		Set<Category> categories = new HashSet<Category>();
		
		for (CategoryEntity categoryEntity : categoryEntities) {
			categories.add(transformCategory(categoryEntity));
		}
		
		return categories;
	}

}
